/*
 * MConexion.java
 *
 * Created on 5 de septiembre de 2006, 08:55 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.formato.modelo;

import ig.formato.dto.SgbdDTO;
import ig.formato.gui.app.PSGDB;
import ig.basic.util.IModelo;
import ig.formato.util.IGFormatoException;
import ig.formato.util.MasterConnection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Properties;
import javax.naming.Context;

/**
 *
 * @author jsanchez
 */
public class MSGDB implements IModelo {

    PSGDB panel = null;
    ig.formato.dao.SgdbDao dao = new ig.formato.dao.SgdbDao();
    ig.formato.dto.SgbdDTO dto = null;

    public MSGDB(PSGDB panel) {
        this.panel = panel;
    }

    public void guardar() throws IGFormatoException {
        Object myObj = this.dao.existe(this.dto);
        String NM = "Sistema Gestor de Base de Dato";
        if (!(myObj instanceof Boolean)) {
            if (this.dto.getId() == null) {
                throw new IGFormatoException("Ya existe una " + NM + " con este nombre", "Cambie el nombre");
            } else if ((myObj instanceof Integer) && !((Integer) myObj).equals(dto.getId())) {
                throw new IGFormatoException("Ya existe una " + NM + " con este nombre", "Cambie el nombre");
            }
        }
        try {
            dao.setAutoCommit(false);
            this.setId(dao.guardar(this.dto));
            dao.commit();
        } catch (IGFormatoException e) {
            dao.rollback();
            throw e;            
        }
    }
        
    public Collection buscar() throws IGFormatoException {
        return dao.buscarTodos();
    }

    public void modificar() throws IGFormatoException {
    }

    public void nuevo() throws IGFormatoException {
        dto = new ig.formato.dto.SgbdDTO();
    }

    public void eliminar() throws IGFormatoException {
        try {
            dao.setAutoCommit(false);
            dao.eliminar(this.dto.getId());
            dao.commit();
        } catch (IGFormatoException e) {
            dao.rollback();
            throw e;            
        }
    }

    public void salir() throws IGFormatoException {
//        MasterConnection.getMasterConnection().closeConnectionMaster();
    }

    public void imprimir() throws IGFormatoException {
    }

    public java.sql.Connection getConectionMaster() throws java.sql.SQLException, ClassNotFoundException, Exception {
        return this.dao.getConnectionIgFormato();
    }

    public Object getTO() throws IGFormatoException {
        return dto;
    }

    public void cancelar() throws IGFormatoException {
        this.setId(this.dto.getId());
    }

    public void setId(Integer id) {
        if (id != null) {
            try {
                dto = (SgbdDTO) this.dao.buscar(id);
            } catch (IGFormatoException ex) {
                ex.printStackTrace();
            }
        }
    }
    public void setClonar(boolean flag)
    {
    }

    public boolean isClonar()
    {
        return false;
    }
}